package com.hrs.core.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrs.common.doMain.PageResultDto;
import com.hrs.common.enums.Status;
import com.hrs.common.result.Result;
import com.hrs.core.mapper.AchieveMapper;
import com.hrs.enity.db.Achieve;
import com.hrs.enity.db.SalaryScale;
import com.hrs.core.service.SalaryScaleService;
import com.hrs.core.mapper.SalaryScaleMapper;
import com.hrs.enity.dto.SalaryViewDto;
import com.hrs.enity.ro.SalaryAddRo;
import com.hrs.enity.ro.SalaryPageRo;
import com.hrs.enity.ro.SalaryReviewRo;
import com.hrs.enity.ro.SalaryUpdateRo;
import com.hrs.util.BeanUtil;
import com.hrs.util.PageUtil;
import com.hrs.util.RequestUserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author 纱雾休比酱
* @description 针对表【t_salary_scale(薪酬标准表)】的数据库操作Service实现
* @createDate 2022-12-05 23:04:49
*/
@Service
public class SalaryScaleServiceImpl extends ServiceImpl<SalaryScaleMapper, SalaryScale>
    implements SalaryScaleService{

    @Autowired
    private SalaryScaleMapper salaryScaleMapper;
    @Autowired
    private AchieveMapper achieveMapper;

    @Override
    public Result<String> add(SalaryAddRo salaryAddRo) {
        SalaryScale salaryScale = new SalaryScale();
        BeanUtils.copyProperties(salaryAddRo,salaryScale);
        salaryScale.setStatus(Status.WAITING);
        if(salaryAddRo.getRegistrantId() == null) {
            salaryScale.setRegistrantId(RequestUserUtil.getRequestUserId());
        }
        this.save(salaryScale);
        return Result.success();
    }

    @Override
    public Result<String> review(SalaryReviewRo salaryReviewRo) {
        Long id = salaryReviewRo.getId();
        SalaryScale salaryScale = this.getById(id);
        if(Objects.isNull(salaryScale)) {
            return Result.error("对象不存在");
        }
        int key = salaryScale.getStatus().getKey();
        if(key != Status.WAITING.getKey()) {
            return Result.error("不是待复核状态");
        }

        BeanUtils.copyProperties(salaryReviewRo,salaryScale);
        salaryScale.setStatus(Status.NORMAL);

        salaryScale.setReviewId(RequestUserUtil.getRequestUserId());
        salaryScale.setReviewTime(LocalDateTime.now());
        this.updateById(salaryScale);
        return Result.success();
    }

    @Override
    public Result<String> updateBySid(SalaryUpdateRo salaryUpdateRo) {
        Long id = salaryUpdateRo.getId();
        SalaryScale salaryScale = this.getById(id);
        if(Objects.isNull(salaryScale)) {
            return Result.error("对象不存在");
        }
        int key = salaryScale.getStatus().getKey();
        if(key != Status.NORMAL.getKey()) {
            return Result.error("不是正常状态");
        }

        BeanUtils.copyProperties(salaryUpdateRo,salaryScale);
        salaryScale.setStatus(Status.WAITING);

        //插入变更人id
        salaryScale.setChangeId(RequestUserUtil.getRequestUserId());
        //插入变更时间
        salaryScale.setChangeTime(LocalDateTime.now());
        salaryScale.setCreateTime(null);
        salaryScale.setRegistrantId(null);

        this.updateById(salaryScale);
        return Result.success();
    }

    @Override
    public Result<SalaryViewDto> getSalaryById(Long id) {
        SalaryViewDto salaryViewDto = salaryScaleMapper.getSalaryById(id);
        return Result.success(salaryViewDto);
    }

    @Override
    public Result<PageResultDto<SalaryViewDto>> pageSalary(SalaryPageRo salaryPageRo) {
        String idKey = StringUtils.isNotBlank(salaryPageRo.getIdKey()) ? "%" + salaryPageRo.getIdKey() + "%" : null;
        String key = StringUtils.isNotBlank(salaryPageRo.getKey()) ? "%" + salaryPageRo.getKey() + "%" : null;
        Date begin = salaryPageRo.getBeginTime();
        Date end = salaryPageRo.getEndTime();
        if(begin == null || end == null) {
            begin = null;
            end = null;
        }
        Integer status = salaryPageRo.getStatus() != null ? salaryPageRo.getStatus().getKey() : null;

        Integer pageNum = salaryPageRo.getPageNum();
        Integer pageSize = salaryPageRo.getPageSize();

        List<SalaryViewDto> salaryViewDtos = salaryScaleMapper.pageSalary((pageNum - 1) * pageSize, pageSize, idKey, key, begin
                , end,status);
        Long total = salaryScaleMapper.totalPage(idKey, key, begin, end,status);
        PageResultDto<SalaryViewDto> result = PageUtil.convert2PageResult(pageNum, pageSize, salaryViewDtos, total);
        return Result.success(result);
    }

    @Override
    public Result<List<SalaryViewDto>> listSalary() {
        List<SalaryViewDto> salaryViewDtos = salaryScaleMapper.listAll(Status.NORMAL.getKey());
        salaryViewDtos.forEach(f -> {
            f.setListShow(f.getName() + "/" + f.getTotal() + "元");
        });
        return Result.success(salaryViewDtos);
    }

    @Override
    public Result<String> deleteSalary(Long id) {
        //查询是否有人员关联
        Achieve achieve = achieveMapper.countBySalaryScaleId(id);
        if(!Objects.isNull(achieve)) {
            return Result.error("与人员有关联，不可删除");
        }
        this.lambdaUpdate().eq(SalaryScale::getId,id).remove();
        return Result.success();
    }
}




